﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Config;

public partial class Theme_Index_stuClassQuery : System.Web.UI.Page
{
    PowerCreator.LMS.Web.CMS.New opNews = new PowerCreator.LMS.Web.CMS.New();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        CheckUser();
        this.Title = config.GetLanagerValueByKey("WebTitle", PowerCreator.LMS.Language.LanagetItemType.WebTitle); //"PowerCreator 学习管理系统";
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        ViewState["stuID"] = login.UserID.ToString();
        if (!IsPostBack)
        {
            BinderRPT();
            BinderCls();
        }
    }

    //检查用户是否登录
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    //我的公告查询字符串
    private string GetNewSearch()
    {
        string sNewIDList = "0";
        string search = "";
        string rtsearch = "";

        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        string sql = "";
        search = "SELECT * FROM TO_UserOfClass where UserID=" + login.UserID;
        dt = ov.Query(search);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sql = "select NewID from TNew where  FKNewTrainclaIDList like '%," + dt.Rows[i]["ClassID"].ToString() + ",%'";
            dt2 = ov.Query(sql);
            for (int k = 0; k < dt2.Rows.Count; k++)
            {
                sNewIDList += "," + dt2.Rows[k]["NewID"].ToString();
            }
            dt2.Clear();
        }
        rtsearch = " NewState=1 and NewID in (" + sNewIDList + ")";
        return rtsearch;
    }
    //绑定我的公告
    private void BinderRPT()
    {
        DataTable dt = new DataTable();
        string orderby = "NewID desc";
        string search = GetNewSearch();
        int iPageSize = 6;
        dt = opNews.Query(search, orderby, iPageSize, 1);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["NewUrl"] == null)
                dt.Rows[i]["NewUrl"] = "OpenNews.aspx?NewsID=" + dt.Rows[i]["NewID"].ToString();
            else if (dt.Rows[i]["NewUrl"].ToString().Length == 0)
                dt.Rows[i]["NewUrl"] = "OpenNews.aspx?NewsID=" + dt.Rows[i]["NewID"].ToString();
        }
        rpt_New.DataSource = dt;
        rpt_New.DataBind();
    }
    //绑定我的班级信息
    private void BinderCls()
    {
        string search = "select distinct a.Class_Id as ClassID, a.Name as ClassName,b.Batch,CreatDate as starTime,a.EndTime from to_class as a ,to_batch as b ,to_courselist as c,to_userofClass as d where a.ClassCourselistID = c.CourseList_id and c.Batch_Id = b.Batch_Id and d.ClassID = a.Class_Id and d.UserID =" + ViewState["stuID"].ToString();
        DataTable dt = new DataTable();
        dt = ov.Query(search);
        rpt_TrainClass.DataSource = dt;
        rpt_TrainClass.DataBind();
     }

    protected void rpt_TrainClass_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "scoreQuery")
        {
            string url = "stuScoresQuery.aspx?ClassID=" + e.CommandArgument.ToString() + "&StuID=" + ViewState["stuID"].ToString() +"&TP=6";
            PowerCreator.LMS.Common.Script.Script.RedirectURL(url);
        }
    }
}
